(() => {

    'use strict';
    goog.provide('Blockly.Arduino.ruilong');
    goog.require('Blockly.Arduino');

//闪灯模块
    Blockly.Arduino.forBlock['ruilong_lamp'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var dropdown_stat = this.getFieldValue('STAT');
        var code = "";
        if (window.isNaN(dropdown_pin)) {
            code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
        } else {
            Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
        }
        code += 'digitalWrite(' + dropdown_pin + ',' + dropdown_stat + ');\n'
        return code;
    };
    //蜂鸣器、继电器、震动马达等
    Blockly.Arduino.forBlock.ruilong_buzzer = Blockly.Arduino.forBlock.ruilong_lamp;
    Blockly.Arduino.forBlock.ruilong_relay = Blockly.Arduino.forBlock.ruilong_lamp;
    Blockly.Arduino.forBlock.ruilong_vibrator = Blockly.Arduino.forBlock.ruilong_lamp;
    Blockly.Arduino.forBlock.ruilong_motor = Blockly.Arduino.forBlock.ruilong_lamp;
    Blockly.Arduino.forBlock.ruilong_usb = Blockly.Arduino.forBlock.ruilong_lamp;
    Blockly.Arduino.forBlock.ruilong_relay_3V = Blockly.Arduino.forBlock.ruilong_lamp;
    Blockly.Arduino.forBlock.ruilong_led = Blockly.Arduino.forBlock.ruilong_lamp;

   

    Blockly.Arduino.forBlock['ruilong_ch9329_mouse_keyboard'] = function () {

        var txpin = Blockly.Arduino.valueToCode(this, 'TXPIN', Blockly.Arduino.ORDER_ATOMIC);
        var press_release = this.getFieldValue('PRESS_RELEASE');
        var key = this.getFieldValue('MOUSE_KEYBOARD');//Blockly.Arduino.valueToCode(this, 'MOUSE_KEYBOARD', Blockly.Arduino.ORDER_ATOMIC);


        Blockly.Arduino.definitions_['defineRL_CH9329_Serial'] = '#include"RL_CH9329_Serial.h"';
        Blockly.Arduino.definitions_['definehid_keys'] = '#include"hid_keys.h"';
      
        Blockly.Arduino.definitions_['defineRL_CH9329_Serial  ch_' + txpin] = 'RL_CH9329_Serial  ch_' + txpin + '(-1, ' + txpin + ');';
        Blockly.Arduino.setups_['setup_  ch_' + txpin] = ' ch_' + txpin + '.begin(9600);';

        


        if (press_release == "KEYBOARD_PRESS") {
            var code = 'ch_' + txpin + '.Keyboard_press(' + key + ');\n ';
        } else if (press_release == "KEYBOARD_RELEASE") {
            var code = 'ch_' + txpin + '.Keyboard_release(' + key + ');\n ';
        } else if (press_release == "KEYBOARD_RELEASE_ALL") {
            var code = 'ch_' + txpin + '.Keyboard_release_all();\n ';
        } else if (press_release == "MOUSE_PRESS") {
            var code = 'ch_' + txpin + '.Mouse_press(' + key + ');\n ';
        } else if (press_release == "MOUSE_RELEASE") {
            var code = 'ch_' + txpin + '.Mouse_release(' + key + ');\n ';
        } else if (press_release == "MOUSE_RELEASE_ALL") {
            var code = 'ch_' + txpin + '.Mouse_release_all();\n ';
        }

        return code;
    };


    //交通灯模块
    Blockly.Arduino.forBlock['ruilong_traffic_light'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var rgb_led_color_r =  this.getFieldValue('RGB_LED_COLOR_R');
        var rgb_led_color_y = this.getFieldValue('RGB_LED_COLOR_Y'); 
        var rgb_led_color_g = this.getFieldValue('RGB_LED_COLOR_G');  

        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
   
        Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '' + '(3);';
        Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
        Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
        
        var code = 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(2, 255*'+rgb_led_color_r+',0,0);\n'
                 + 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(1, 255*'+rgb_led_color_y+',255*'+rgb_led_color_y+',0);\n'
                 + 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(0, 0,255*'+rgb_led_color_g+',0);\n';
                 + 'rgb_display_' + dropdown_rgbpin + '.show();\n';
                 + 'delay(100);\n';
        return code;
    };

    })();